/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.brain.core.manager;

import java.io.Serializable;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.springframework.stereotype.Service;

/**
 *
 * @author Indra
 */
@Service
public class UtilEmailManager implements Serializable {

    private String miCorreo = "web.brain.jbiss";
    private String miPassword = "web.brain.jbiss";
    private String servidorSalida = "smtp.gmail.com";
    private String puertoSalida = "465";
    private String nombreRemitente = "SISTEMA.GESTION.COMPETENCIA";
    
    public static void main(String[] a) {
        System.out.println(new UtilEmailManager().enviarEmail("PRUEBA 2", "ESTE ES EL CUERPO", "ronald.chang.jbiss@gmail.com"));
    }

    public String enviarEmail(String asunto, String cuerpo,
            InternetAddress... destinos) {
        String retorno = null;
        try {
            Session session = obtenerSession(obtenerConfiguracion());
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(nombreRemitente));            
            message.setRecipients(Message.RecipientType.TO, destinos);
            message.setSubject(asunto);
            message.setText(cuerpo);
            message.removeHeader("Content-Type");
            message.addHeader("Content-Type", "text/html");
            Transport.send(message);

            retorno = "Envio de email satisfactorio!!!";

        } catch (MessagingException e) {
            retorno = e.getMessage();
        }
        return retorno;
    }
    
    public String enviarEmail(String asunto, String cuerpo,
            String... destinos) {
        String retorno = null;
        try {
            InternetAddress[] ias = new InternetAddress[destinos.length];            
            int i=0;
            for (String r : destinos) {                
                ias[i] = new InternetAddress(r);               
            }
            
            Session session = obtenerSession(obtenerConfiguracion());
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(nombreRemitente));            
            message.setRecipients(Message.RecipientType.TO, ias);
            message.setSubject(asunto);
            message.setText(cuerpo);
            message.removeHeader("Content-Type");
            message.addHeader("Content-Type", "text/html");
            Transport.send(message);

            retorno = "Envio de email satisfactorio!!!";

        } catch (MessagingException e) {
            retorno = e.getMessage();
        }
        return retorno;
    }

    private Properties obtenerConfiguracion() {
        Properties props = new Properties();
        props.put("mail.smtp.host", servidorSalida);
        props.put("mail.smtp.socketFactory.port", puertoSalida);
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", puertoSalida);

        return props;
    }

    private Session obtenerSession(Properties props) {
        Session session = Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(miCorreo,
                                miPassword);
                    }
                });

        return session;
    }

    public String getMiCorreo() {
        return miCorreo;
    }

    public void setMiCorreo(String miCorreo) {
        this.miCorreo = miCorreo;
    }

    public String getMiPassword() {
        return miPassword;
    }

    public void setMiPassword(String miPassword) {
        this.miPassword = miPassword;
    }

    public String getServidorSalida() {
        return servidorSalida;
    }

    public void setServidorSalida(String servidorSalida) {
        this.servidorSalida = servidorSalida;
    }

    public String getPuertoSalida() {
        return puertoSalida;
    }

    public void setPuertoSalida(String puertoSalida) {
        this.puertoSalida = puertoSalida;
    }

    public String getNombreRemitente() {
        return nombreRemitente;
    }

    public void setNombreRemitente(String nombreRemitente) {
        this.nombreRemitente = nombreRemitente;
    }
    
    
}
